<?php
namespace app\admin\model;

use app\common\model\CommonBase;

class Shareholder extends Base{

    protected $name = 'shareholder';

    const CRIO_URL = 'http://emweb.securities.eastmoney.com/PC_HSF10/ShareholderResearch/PageAjax?code=';

    public function initialize(){

        $this->shareholderinfo = new Shareholderinfo();
        $this->shareholderDb = db('shareholder');
    }

    //删除
    public function delStockShareholder($sid=0, $stock_code=''){
        $this->where(['sid'=>$sid,'stock_code'=>$stock_code])->delete();
    }

    //获取前十大股东及高管
    public function crioShareholder($arr=[],$sid=''){

    	$data = null;
    	$data = $this->getUrl(self::CRIO_URL.$arr['jys'].$arr['stock_code'], 0);
    	$data = json_decode($data, true);
    	foreach ($data['sdltgd'] AS $vsdltgd) {

    		$arrData = [];
    		$arrData['shareholder_name'] = $vsdltgd['HOLDER_NAME'];
    		$arrData['shareholder_identity'] = $vsdltgd['HOLDER_TYPE'];
    		$arrData['shareholding_ratio'] = sprintf("%.2f", $vsdltgd['FREE_HOLDNUM_RATIO']);
    		$arrData['is_crio'] = 0;
    		$arrData['organization_type'] = null;
    		$arrData['organization_type'] = $this->identityType($vsdltgd['HOLDER_NAME']);
    		if($arrData['organization_type'] == null){
    			$arrData['organization_type'] = 6;
    		}
    		$arrData['is_circulate'] = 1;
    		$arrData['rank'] = $vsdltgd['HOLDER_RANK'];
    		$arrData['crio_type'] = 0;
            $arrData['shareholding_ratio_type'] = trim($vsdltgd['HOLD_NUM_CHANGE']);
			$detailShareholderWhere = $arrData;
			$detailShareholderWhere['sid'] = $sid;
			$detailShareholderWhere['stock_code'] = $arr['stock_code'];
    		$reArrData = $this->detailShareholder($detailShareholderWhere);
    		if(!empty($reArrData['shareholder_id'])){
				$this->where(['shareholder_id'=>$reArrData['shareholder_id']])->update($arrData);
    		}else{
				$arrData['sid'] = $sid;
				$arrData['stock_code'] = $arr['stock_code'];
    			$this->insert($arrData);
    		}
    	}
    	//股东信息
    	$this->shareholderinfo->handleStockShareholderinfo($data['gdrs'], $sid, $arr['stock_code']);
    	//实际控制人
    	$arrData = [];
    	$arrData['shareholder_name'] = trim($data['sjkzr'][0]['HOLDER_NAME']);
    	$arrData['shareholder_identity'] = '实际控制人';
    	$arrData['crio_type'] = 2;
    	$where = ['sid'=>$sid, 'stock_code'=>$arr['stock_code'], 'shareholder_name'=>$arrData['shareholder_name']];
    	$reArrData = null;
    	$reArrData = $this->detailShareholder($where);
		if(!empty($reArrData['shareholder_id'])){
			$this->where(['shareholder_id'=>$reArrData['shareholder_id']])->update($arrData);
    	}else{
			$arrData['sid'] = $sid;
			$arrData['stock_code'] = $arr['stock_code'];
    		$this->insert($arrData);
    	}
    }

    //股东排序详情
    public function detailShareholder($where=[], $order=''){

        $reVal = null;
    	$reVal = $this->where($where)->order($order)->find();
        if($reVal != null){
            return $reVal->toArray();
        }
		return $reVal;
    }

    //获取身份及类型
    protected function identityType($str = ''){

    	$arr = [
    		0=>['name'=>'全国社保基金','code'=>1],
			1=>['name'=>'社保基金','code'=>1],
			2=>['name'=>'社保','code'=>1],
			3=>['name'=>'国资委','code'=>5],
			4=>['name'=>'财政','code'=>5],
			5=>['name'=>'国务院','code'=>5],
			6=>['name'=>'全国','code'=>5],
			7=>['name'=>'中国','code'=>5],
			8=>['name'=>'国有资产','code'=>5],
			9=>['name'=>'国家','code'=>5],
			10=>['name'=>'证券金融','code'=>5],
    		11=>['name'=>'银行保险','code'=>2],
    		12=>['name'=>'银行理财','code'=>3],
    		13=>['name'=>'保险理财','code'=>2],
    		14=>['name'=>'基金理财','code'=>7],
    		15=>['name'=>'理财基金','code'=>7],
    		16=>['name'=>'ETF','code'=>8],
    		17=>['name'=>'保险银行','code'=>2],
    		18=>['name'=>'理财银行','code'=>3],
    		19=>['name'=>'理财保险','code'=>2],
    		20=>['name'=>'理财基金','code'=>7],
    		21=>['name'=>'证券金融','code'=>5],
    		22=>['name'=>'保险','code'=>2],
    		23=>['name'=>'基金','code'=>7],
    	];
    	$reStr = '';
    	$tmp = [];
    	$str = trim($str);
    	foreach ($arr AS $k=>$v) {	
    		$reStr = strpos($str, $v['name']);
    		if($reStr!==false){
    			return $v['code'];
    		}
    	}
    }

    //股东及管理层-列表
    public function shareholderList($arr=[], $seniorExecutive=0){

        $reVal = [];
        $reVal = $this->shareholderDb->where(['sid'=>$arr['sid'], 'stock_code'=>$arr['stock_code'], 'organization_type'=>['GT', $seniorExecutive]])->order('organization_type ASC')->select();
        return $reVal;
    }
}